package com.axzhengxin.platform.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.axzhengxin.platform.business.domain.*;
import com.axzhengxin.platform.business.service.IBizApproveInfoService;
import com.axzhengxin.platform.business.service.IBizApplyInfoService;
import com.axzhengxin.platform.business.service.IBizFlowInfoService;
import com.axzhengxin.platform.common.service.IDbExecService;
import com.axzhengxin.platform.common.util.SmsSendService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.text.ParseException;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 待审核订单Controller
 *
 * @author axzhengxin
 * @date 2021-02-22
 */
@Slf4j
@RestController
@RequestMapping("/distribute/bizFlowInfo")
public class BizFlowInfoController extends BaseController {
    @Autowired
    private IBizApplyInfoService bizApplyInfoService;

    @Autowired
    private IBizFlowInfoService biFlowInfoService;

    @Autowired
    private SmsSendService smsSendService;

    @Resource(name = "threadPoolTaskExecutor")
    private ThreadPoolTaskExecutor threadPoolExecutor;

    @Autowired
    private ISysUserService sysUserService;


    /**
     * 获取待审核订单详细信息
     */
    /**
     * @PreAuthorize("@ss.hasPermi('distribute:bizFlowInfo:query')")
     */
    @GetMapping(value = "/{reqId}")
    public AjaxResult getInfo(@PathVariable("reqId") String reqId) {
        return AjaxResult.success(bizApplyInfoService.selectBizApplyInfoById(reqId));
    }

    /**
     * 执行分配操作
     * @return
     */
    /**
     * @PreAuthorize("@ss.hasPermi('distribute:bizFlowInfo:distributeApply')")
     */
    @PostMapping("/distributeApply")
    public AjaxResult distributeApply(@RequestBody BizApplyInfo bizApplyInfo) {
        //已分配 未处理
        String flowUid = bizApplyInfo.getFlowUid();
        String reqId = bizApplyInfo.getReqId();
        String userId = bizApplyInfo.getUserId();
        int i = biFlowInfoService.distributeApply(userId, reqId, flowUid);
        //异步发送短信提醒
        if (i > 0) {
            log.info("--------------------开始异步发送短信提醒-----------------------");
            threadPoolExecutor.submit(() -> {
                BizApplyInfo bizApplyInfo1 = bizApplyInfoService.selectBizApplyInfoById(reqId);
                String customerName = bizApplyInfo1.getCustomerName();
                String msg = "您有一笔来自|" + customerName + "|的待处理业务，请您尽快登录企业服务中心处理。";
                SysUser sysUser = sysUserService.selectUserById(Long.parseLong(userId));
                String phonenumber = sysUser.getPhonenumber();
                try {
                    smsSendService.sendMsg("prod", phonenumber, null,msg, "412607");
                    log.info("发送提醒短信：-------------"+msg);
                } catch (Exception e) {
                    log.info("短信发送失败"+msg);
                    e.printStackTrace();
                }
            });

        }
        return toAjax(i);
    }

    /**
     *查询流程记录
     */
    /**
     * @PreAuthorize("@ss.hasPermi('distribute:bizFlowInfo:getRecordList')")
     */
    @RequestMapping("/getRecordList")
    public Record getRecordList(String reqId, String flowUid) {
        Record record = biFlowInfoService.getRecordList(reqId, flowUid);
        return record;
    }

    /**
     *  进行审核操作、将审核人、审核时间...录入流程记录
     * @param bizFlowInfo
     * @return
     */
    /**
     * @PreAuthorize("@ss.hasPermi('distribute:bizFlowInfo:auditing')")
     */
    @RequestMapping("/auditing")
    public AjaxResult auditing(@RequestBody BizFlowTask bizFlowTask) {
        try {
            return biFlowInfoService.auditing(bizFlowTask);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }


    /**
     * 收回
     */
    @PreAuthorize("@ss.hasPermi('distribute:bizFlowInfo:getBackApply')")
    @RequestMapping("/getBackApply")
    public AjaxResult getBackApply(String flowUid) {
        biFlowInfoService.getBackApply(flowUid);
        return AjaxResult.success();
    }

    /**
     * 退回
     */
    @PreAuthorize("@ss.hasPermi('distribute:bizFlowInfo:backApply')")
    @RequestMapping("/backApply")
    public AjaxResult backApply(String flowUid) {
        try {
            biFlowInfoService.backApply(flowUid);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }


}
